package com.qiyadeng.company.biz;

import java.net.MalformedURLException;
import java.net.URL;
import java.rmi.RemoteException;
import java.util.List;


import org.apache.axis.AxisFault;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.common.base.Strings;
import com.mmt.bean.model.A1062Info;
import com.mmt.bean.model.SmsInfoReq;
import com.mmt.bean.model.TicketInfo;
import com.mmt.bean.model.UserInfo;
import com.mmt.dao.MmtInterfaceHttpBindingStub;
import com.mmt.dao.MmtInterfaceLocator;

public class MmtService {
	
	Logger logger = LoggerFactory.getLogger(MmtService.class);
	private MmtInterfaceHttpBindingStub client;

	//构造函数
	public MmtService(){
		URL url = null;
		try {
			url = new URL(MmtConstant.SERVICE_ADDRES);
		} catch (MalformedURLException e) {
			logger.warn("mmt service url is malformed:"+MmtConstant.SERVICE_ADDRES+"("+e.getMessage()+")");
			e.printStackTrace();
		}
		MmtInterfaceLocator mmt = new MmtInterfaceLocator();
		try {
			this.client = new MmtInterfaceHttpBindingStub(url, mmt);
		} catch (AxisFault e) {
			logger.warn("create axis client failure:" + e.getMessage());
			e.printStackTrace();
		}
	}
	
	public MmtService(String smsWsdl){
		URL url = null;
		try {
			url = new URL(MmtConstant.SMS_ADDRESS);
		} catch (MalformedURLException e) {
			logger.warn("mmt service url is malformed:"+MmtConstant.SMS_ADDRESS+"("+e.getMessage()+")");
			e.printStackTrace();
		}
		MmtInterfaceLocator mmt = new MmtInterfaceLocator();
		try {
			this.client = new MmtInterfaceHttpBindingStub(url, mmt);
		} catch (AxisFault e) {
			logger.warn("create axis client failure:" + e.getMessage());
			e.printStackTrace();
		}
	}
	//查询卡余额
	public String getCardBalance(String cardNo,String password){
		String result = "";
		try {
			//result = client.selectCardinfo2(MmtConstant.MERMO, MmtConstant.LOGINID, MmtConstant.LOGINPASSWD, cardNo, password);
			//result = client.selectCardinfo2("2026", "admin", "admin", cardNo, password);
			result = client.selectCardinfo(MmtConstant.SUPER_MERNO,MmtConstant.SUPER_LOGIN, MmtConstant.SUPER_PASSWORD, cardNo, password);
			logger.info("getCardBalance result:"+result);
			//解析xml
			RespCardInfo2 rci = RespCardInfo2.getCardInfo2FromXmlString(result);
			return rci.getInformation().getCardbalance();
			
		} catch (RemoteException e) {
			logger.warn("selectCardInfo2 failure:" + e.getMessage());
			e.printStackTrace();
		}
		return result;
	}
	
	//查询卡积分
	public String getCardIntegral(String cardNo,String password){
		String result = "";
		try {
			//result = client.selectCardinfo2(MmtConstant.MERMO, MmtConstant.LOGINID, MmtConstant.LOGINPASSWD, cardNo, password);
			//result = client.selectCardinfo2("2026", "admin", "admin", cardNo, password);
			result = client.selectCardinfo(MmtConstant.SUPER_MERNO,MmtConstant.SUPER_LOGIN, MmtConstant.SUPER_PASSWORD, cardNo, password);
			logger.info("getCardIntegral result:"+result);
			//解析xml
			RespCardInfo2 rci = RespCardInfo2.getCardInfo2FromXmlString(result);
			return rci.getInformation().getCardintegral();
			
		} catch (RemoteException e) {
			logger.warn("getCardIntegral failure:" + e.getMessage());
			e.printStackTrace();
		}
		return result;
	}
	
	//查询用户信息
	public ResSelectUser selectUserInfo(String card,String cerNo,String mobie,String email){
		ResSelectUser user = null;
		try {
			String result = client.selectUserInfo(MmtConstant.MERMO, MmtConstant.LOGINID, MmtConstant.LOGINPASSWD, card, cerNo, mobie, email);
			logger.debug("select user info result:" + result);
			user = ResSelectUser.getResSelectUserFromXml(result);
			return user;
			
		} catch (RemoteException e) {
			logger.warn("select User Info failure:"+e.getMessage());
			e.printStackTrace();
		}
		return user;
	}
	
	//新增新会员
	public boolean addUserInfo(String name,String idType,String idNo,String phone,String email){
		boolean success = false;
		UserInfo userInfo = new UserInfo();
		userInfo.setTerm_no(MmtConstant.TERM_MERMO);
		userInfo.setMer_no(MmtConstant.MERMO);
		userInfo.setLoginid(MmtConstant.LOGINID);
		userInfo.setLoginpwd(MmtConstant.LOGINPASSWD);
		
		userInfo.setName(name);
		userInfo.setId_type(idType);
		userInfo.setId_no(idNo);
		userInfo.setMobile(phone);
		userInfo.setEmail(email);
		
		userInfo.setSex("0");//默认男性
		
		//TODO 如果身份证18位，可判断男女
		//TODO 卡号isnull
		try {
			String result = client.addUserInfo(MmtConstant.MERMO, MmtConstant.TERM_MERMO, MmtConstant.LOGINID, MmtConstant.LOGINPASSWD, userInfo);
			logger.info("addUserInfo response:" + result);
			if(!Strings.isNullOrEmpty(result)&&result.indexOf("成功")>-1){
				success = true;
			}
		} catch (RemoteException e) {
			logger.warn("addUserInfo failure:" + e.getMessage());
			e.printStackTrace();
		}
		return success;
	}
	
	
	//修改密码 pwd1原始密码
	public String modifyPassword(String card,String pwd1,String pwd2,String pwd3,String flag){
		String message = "";
		try {
			String result = client.updatePwdNew(MmtConstant.MERMO, MmtConstant.LOGINID, MmtConstant.LOGINPASSWD, card, pwd1, pwd2, pwd3, flag);
			if(!Strings.isNullOrEmpty(result)&&result.indexOf("成功")>-1){
				message = "0000:成功";
			}
		} catch (RemoteException e) {
			logger.warn("modify password failure:" + e.getMessage());
			e.printStackTrace();
		}
		return message;
	}
	
	//卡内电子券查询
	
	public List<RespQueryTicket> queryCardTicket(String cardNo){
		List<RespQueryTicket> rt = null;
		TicketInfo ticketInfo = new TicketInfo();
		ticketInfo.setTermMerno(MmtConstant.MERMO);
		ticketInfo.setLoginid(MmtConstant.LOGINID);
		ticketInfo.setLoginpwd(MmtConstant.LOGINPASSWD);
		ticketInfo.setCardNo(cardNo);
		try {
			String result = client.queryCardTicket(ticketInfo);
			logger.debug("queryCardTicket result:"+result);
			rt = RespQueryTicket.getRespQueryTicketFromXml(result);
		} catch (RemoteException e) {
			logger.warn("query card ticket failure:"+ e.getMessage());
			e.printStackTrace();
		}
		return rt;
	}
	
	
	//发放电子券
	public RespGrantTicket grantTicket(String phone){
		RespGrantTicket respGrantTicket = null;
		A1062Info a1062Info = new A1062Info();
		a1062Info.setTermMerno(MmtConstant.MERMO);
		a1062Info.setLoginid(MmtConstant.LOGINID);
		a1062Info.setLoginpwd(MmtConstant.LOGINPASSWD);
		a1062Info.setTermNo(MmtConstant.TERM_MERMO);
		a1062Info.setSendObjType("0");//手机号
		a1062Info.setSendObj(phone);//手机号
		a1062Info.setSendMessageType("1");//手机
		a1062Info.setTicketType(MmtConstant.TICKET_TYPE);
		a1062Info.setNum("1");
		try {
			String result = client.grantTicke(a1062Info);
			logger.info("grant ticket:"+result);
			respGrantTicket = RespGrantTicket.getRespGrantTicketFromXml(result);
		} catch (RemoteException e) {
			logger.warn("grant ticket failure:"+e.getMessage());
			e.printStackTrace();
		}
		return respGrantTicket;
	}
	
	//发送短信
	public String sendSms(String phone,String content){
		SmsInfoReq smsInfo = new SmsInfoReq();
		smsInfo.setContent(content+"【会员通】");
		smsInfo.setInfoAddr(phone);
		smsInfo.setSendType("0");
		smsInfo.setSendMerNo(MmtConstant.SMS_MERNO);
		smsInfo.setSendTermNo(MmtConstant.SMS_TERM_MERNO);
		smsInfo.setLoginid(MmtConstant.SMS_LOGIN);
		smsInfo.setLoginpwd(MmtConstant.SMS_PASSWD);
		try {
			client.sendSMS(smsInfo);
		} catch (RemoteException e) {
			logger.warn("send sms failure:"+e.getMessage());
			e.printStackTrace();
		}
		return "";
	}
}
